/**
 * 
 */
package org.ucn.dmaa0914.gruppe6;

import java.util.ArrayList;

/**
 * @author Kim Johannsen
 * @version 09-12-2014
 */
public class EmployeeCtr extends CrudController<Employee> {
	private static EmployeeCtr instance;
	private int currentEmployee;
	
	public static EmployeeCtr getInstance()
	{
		if( instance == null )
			instance = new EmployeeCtr();
		
		return instance;
	}
	
	public int getCurrentEmployee()
	{
		return currentEmployee;
	}
	
	public void setCurrentEmployee(int empId)
	{
		currentEmployee = empId;
	}

	@Override
	protected SearchFilter<Employee> getSearchFilter() {
		return new EmployeeSearchFilter();
	}

	@Override
	protected Employee newObject() {
		return new Employee();
	}

	@Override
	public String format(int id) {
		return find(id).getUsername();
	}
	
	public boolean login(String username, String password)
	{
		ArrayList<Employee> employees = search(username);
		if( employees.size() == 1 )
		{
			if( employees.get(0).getUsername().equals(username) && employees.get(0).getPassword().equals(password) )
			{
				setCurrentEmployee(employees.get(0).getId());
				return true;
			}
		}
		return false;
	}

	public void logout()
	{
		setCurrentEmployee(0);
	}
	
	public void updateEmployee(int id, String username, String password, String phone)
	{
		Employee e = find(id);
		e.setUsername(username);
		e.setPassword(password);
		e.setPhone(phone);
	}
}
